#!/usr/bin/env python
# encoding: utf-8
"""
merImport.py

Created by Brant Faircloth on 2007-08-15.
Copyright (c) 2007 Brant C. Faircloth. All rights reserved.

Imports data from Wang's MER estimator to a dbase with columns identical to the data for querying (in this case MySQL)
"""

import string,MySQLdb,pdb,re

def insertData(c, handle):
    '''Pair(Indv1:Indv2)   Mean(PHI)     SD(PHI) Mean(DELTA)   SD(DELTA)     Mean(R)       SD(R)'''
    for line in handle:
        # find any line with whitespace and replace whitespace with tab
        find = re.compile(':|\s+[^A-Za-z0-9-\n]')
        #pdb.set_trace()
        line = find.sub('\t',line)
        line    = line.split('\t')
        if line[-1] == '-\n':
            line = [line[0],line[1],'Null','Null','Null','Null','Null','Null\n']
        line[0] = line[0].lstrip('#')
        line[1] = line[1].lstrip(':#')
        line[6] = line[6].lstrip(' ')
        try:
            line[7] = line[7].rstrip('\n')
        except:
            pdb.set_trace()
        contents = ()
        for element in line:
            contents += (element,)
        print line
        #pdb.set_trace()
        query = (("INSERT INTO 03_relatedness VALUES ('%s','%s',%s,%s,%s,%s,%s,%s)") % (contents))
        c.execute(query)
    # parse the record
    

def main():
    # make database connection
    database = MySQLdb.connect("parameters")
    c = database.cursor()
    # open the file to read
    handle = open('./RL_OUTPT','rU')
    insert = insertData(c,handle)
    handle.close()
    c.close()
    


if __name__ == '__main__':
    main()
